package com.bjsxt.ssm.service.impl;

import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.ssm.bean.TbResRo;
import com.bjsxt.ssm.bean.TbRole;
import com.bjsxt.ssm.bean.TbUserRole;
import com.bjsxt.ssm.mapper.TbResRoMapper;
import com.bjsxt.ssm.service.TbResRoService;

@Service
public class TbResRoServiceImpl implements TbResRoService {

	@Autowired
	private TbResRoMapper tbResRoMapper;
	
	@Override
	public int useAppendRole(Integer[] roleIds, Integer[] resourceIds) {
		if(ArrayUtils.isEmpty(roleIds)||ArrayUtils.isEmpty(resourceIds)){
			return -1;
		}
		for (Integer roleId : roleIds) {
			for (Integer resourceId : resourceIds) {
				 TbResRo resRo=new TbResRo();
				 resRo.setResourceId(resourceId);
				 resRo.setRoleId(roleId);
				int has =tbResRoMapper.checkTbResRole(resRo);
				if(0 == has){
					tbResRoMapper.insertTbResRole(resRo);
				}
			}
			
		}
				return 1;
	}

	@Override
	public int useOverWriteRole(Integer[] roleIds, Integer[] resourceIds) {
		if(ArrayUtils.isEmpty(roleIds) || ArrayUtils.isEmpty(resourceIds) ){
			return -1 ;
		}
		for (Integer roleId : roleIds) {
			 int r = tbResRoMapper.deleteTbResRo(roleId);
			 for (Integer resourceId : resourceIds) {
				TbResRo resRo=new TbResRo();
				
				resRo.setResourceId(resourceId);
				resRo.setRoleId(roleId);
				tbResRoMapper.insertTbResRole(resRo); 
			}
		}
		
		
		return 1;
	}


	@Override
	public List<TbResRo> selectTbResById(int roleId) {
		if(roleId<0){
			return null;
		}
		return this.tbResRoMapper.selectTbResById(roleId);
	}

}
